#ifndef CLOG_H
#define CLOG_H

/**
@author antrykot...
*/

#include "main_inc.h"
#include "def_ver.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//#include <QString>

#define MOD_APPEND	1
#define MOD_OVER	2

#define LOG (*CLog::_log)
#define MFUNC    __FUNCTION__
#define MLINE    __LINE__
#define MFILE    __FILE__

#define LOGP1(X) (*CLog::_log).Print((char*)X)
#define LOGP2(X,Y) (*CLog::_log).Print((char*)X,Y)
#define LOGP3(X,Y,Z) (*CLog::_log).Print((char*)X,Y,Z)
#define LOGP4(X,Y,Z,A) (*CLog::_log).Print((char*)X,Y,Z,A)
#define LOGP5(X,Y,Z,A,B) (*CLog::_log).Print((char*)X,Y,Z,A,B)
#define LOGP6(X,Y,Z,A,B,C) (*CLog::_log).Print((char*)X,Y,Z,A,B,C)
/*
string inline Sfile(QString x)
{
    int y = x.lastIndexOf('f',0);
    x = x.right(y);
    return x.toStdString();
}
*/
#define SFILE    Sfile(__FILE__).c_str()



extern i32 errcode1;

class CLog
{
	public:
		CLog();
		~CLog();

        static int Create();
        static int Destroy();

		int StartLog (string file_name,uint32_t mode = 0);
		int CloseLog ();

		int SetMinPriority(uint32_t priority);
		int WriteDate(uint32_t write);
		int ClearLog();

        int Print(char *fmt,...);
        int Print(uint32_t prior, char *fmt,...);

        static int Stampin(char *func_name);
        static int Stampout(char *func_name);

        int Mark(char *file,char *func_name,uint32_t line);
		static CLog *_log;
		uint32_t _console_out;
	protected:
        int WriteBuffer(char *buff,uint32_t prior = 0);

		int WriteHeader(string prog_name = VER_APPNAME,string prog_author = VER_AUTHOR,uint32_t major_ver = VER_MAJOR,
				uint32_t minor_ver = VER_MINOR,uint32_t rev = VER_REV);
		int WriteEndMsg();

                string DateToString();

                void TimeToStr(char *buffer);

        uint32_t _status;
        uint32_t _min_prior;
		FILE *_file;
		uint32_t _tabs;
		uint32_t _function_tree;
};

/// DOCUMENTATION :
///     priority :
///         0 means critical
///         1 means less important
///         2 ...
///     ---------------------------
///

#define GLERR {	errcode1 = glGetError();											 \
				if (errcode1 != GL_NO_ERROR)LOG.Print(0,(char*)"GLERROR in %s %d %s >>> %s\n",	\
				__FILE__,__LINE__,__FUNCTION__, gluErrorString(errcode1));}


#endif
